package com.wentao.mall.controller;

import com.wentao.mall.commons.Constant;
import com.wentao.mall.commons.Result;
import com.wentao.mall.exception.MallException;
import com.wentao.mall.exception.MallExceptionEnum;
import com.wentao.mall.model.po.User;
import com.wentao.mall.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RestController
public class UserController {
    @Resource
    private UserService userService;

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public Result register(@RequestParam("userName") String userName,@RequestParam("password") String password){
        if(!StringUtils.hasText(userName)){
            return Result.error(MallExceptionEnum.NEED_USERNAME);
        }
        if(!StringUtils.hasText(password)){
            return Result.error(MallExceptionEnum.NEED_PASSWORD);
        }
        if(password.length()<6){
            return Result.error(MallExceptionEnum.ILLEGAL_PASSWORD);
        }
        userService.register(userName,password);
        return Result.success("注册成功");
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result login(@RequestParam("userName") String userName, @RequestParam("password") String password, HttpSession httpSession){

        if (StringUtils.hasText(userName)&&StringUtils.hasText(password)) {
            User user = userService.login(userName, password);
            user.setPassword(null);     //前端不需要返回密码，我们将其设置为空，防止泄露
            httpSession.setAttribute(Constant.CURRENT_USER, user);
            return Result.success(user);
        }
        else
            return  Result.error(MallExceptionEnum.NEED_USERNAME_PWD);
    }

    @ApiOperation("用户更新签名")
    @PostMapping("/user/update")
    public Result signature(@RequestParam("signature") String signature,HttpSession httpSession){
        User user=(User) httpSession.getAttribute(Constant.CURRENT_USER);
        if (user==null){
            return Result.error(MallExceptionEnum.NEED_LOGIN);
        }
        user.setPersonalizedSignature(signature);
        userService.personalSignature(signature,user);
        return Result.success("更新成功");
    }

    @ApiOperation("用户注销")
    @PostMapping("/user/logout")
    public Result logout(HttpSession httpSession){
        httpSession.invalidate();;
        return Result.success("注销成功");
    }

    @ApiOperation("管理员用户登录")
    @PostMapping("/adminLogin")
    public Result adminLogin(@RequestParam("userName") String userName, @RequestParam("password") String password, HttpSession httpSession){

        if (StringUtils.hasText(userName)&&StringUtils.hasText(password)){
            User user = userService.login(userName, password);
            user.setPassword(null);     //前端不需要返回密码，我们将其设置为空，防止泄露
            if (!user.getRole().equals(Constant.IS_ADMIN)){
                throw new MallException(MallExceptionEnum.NEED_ADMIN);
            }
            httpSession.setAttribute(Constant.CURRENT_USER, user);
            return Result.success(user);
        }
        else
            return  Result.error(MallExceptionEnum.NEED_USERNAME_PWD);
    }


}
